package com.qujia.integral.service.impl;

import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.integral.domain.IntegralRule;
import com.qujia.integral.domain.IntegralRuleDetail;
import com.qujia.integral.domain.IntegralShopPic;
import com.qujia.integral.domain.IntegralTimeRuleDetail;
import com.qujia.integral.mapper.IntegralRuleDetailMapper;
import com.qujia.integral.mapper.IntegralRuleMapper;
import com.qujia.integral.mapper.IntegralTimeRuleDetailMapper;
import com.qujia.integral.service.IIntegralRuleService;
import com.qujia.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 积分规则Service业务层处理
 *
 * @author qujia
 * @date 2021-02-24
 */
@Service
public class IntegralRuleServiceImpl implements IIntegralRuleService
{
    @Autowired
    private IntegralRuleMapper integralRuleMapper;
    @Autowired
    private IntegralRuleDetailMapper integralRuleDetailMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private IntegralTimeRuleDetailMapper integralTimeRuleDetailMapper;
    /**
     * 查询积分规则
     *
     * @param id 积分规则ID
     * @return 积分规则
     */
    @Override
    public IntegralRule selectIntegralRuleById(Long id)
    {
        //查询油品数据
        IntegralRule integralRule =integralRuleMapper.selectIntegralRuleById(id);
        IntegralRuleDetail  detail  = new  IntegralRuleDetail();
        detail.setParentId(id);
        List<IntegralRuleDetail> list =  integralRuleDetailMapper.selectIntegralRuleDetailList(detail);
        integralRule.setItemsDetail(list);
        //查询图片信息
        List<IntegralShopPic> shopPicList =new ArrayList<>();
        if(integralRule!=null && integralRule.getImage1() !=null && !integralRule.getImage1().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("1");
            pic.setUrl(integralRule.getImage1());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage2() !=null && !integralRule.getImage2().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("2");
            pic.setUrl(integralRule.getImage2());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage3() !=null && !integralRule.getImage3().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("3");
            pic.setUrl(integralRule.getImage3());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage4() !=null && !integralRule.getImage4().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("4");
            pic.setUrl(integralRule.getImage4());
            shopPicList.add(pic);
        }
        if(integralRule!=null && integralRule.getImage5() !=null && !integralRule.getImage5().equals("")){
            IntegralShopPic pic =new IntegralShopPic();
            pic.setName("5");
            pic.setUrl(integralRule.getImage5());
            shopPicList.add(pic);
        }
        integralRule.setImgFileList(shopPicList);
        return integralRule;
    }

    /**
     * 查询积分规则列表
     *
     * @param integralRule 积分规则
     * @return 积分规则
     */
    @Override
    public List<IntegralRule> selectIntegralRuleList(IntegralRule integralRule)
    {
        return integralRuleMapper.selectIntegralRuleList(integralRule);
    }

    /**
     * 新增
     *
     * @param integralRule
     * @return 结果
     */
    @Override
    public int insertIntegralRule(IntegralRule integralRule)
    {
        if(integralRule.getIntegralProportion()==null){
            integralRule.setIntegralProportion("1");
        }
        if(integralRule.getDatePickerTime()==null){
            integralRule.setDatePickerTime("");
        }
        if(integralRule.getDatePicker()==null){
            integralRule.setDatePicker("");
        }
        if(integralRule.getImgFileList()!=null &&  integralRule.getImgFileList().size()>0){
            List<IntegralShopPic> imgFileList =integralRule.getImgFileList();
            for (int j=0;j<imgFileList.size();j++){
                if(j==0){
                    integralRule.setImage1(imgFileList.get(j).getUrl());
                }else if(j==1){
                    integralRule.setImage2(imgFileList.get(j).getUrl());
                }else if(j==2){
                    integralRule.setImage3(imgFileList.get(j).getUrl());
                }else if(j==3){
                    integralRule.setImage4(imgFileList.get(j).getUrl());
                }else if(j==4){
                    integralRule.setImage5(imgFileList.get(j).getUrl());
                }
            }
        }
        System.out.println("--------------------"+integralRule.getIntegralEmptyTime());
        System.out.println("--------------------"+integralRule.getEmptyDate());
        int i =integralRuleMapper.insertIntegralRule(integralRule);
      //  IntegralRule rule=  integralRuleMapper.insertIntegralRule(integralRule);
        //判断规则明细
        if(integralRule.getItemsDetail()!=null &&  integralRule.getItemsDetail().size()>0){
            List<IntegralRuleDetail> list =integralRule.getItemsDetail();
            for (IntegralRuleDetail detail : list){
//                if(detail.getOilName()!=null && detail.getOilName()!=""){
//                    //查询当前油品对应的
//                    String dataValue= dictDataMapper.selectDictValue("oil_name",detail.getOilName());
//                    detail.setOilType(dataValue);
//                }else{
//                    i=0;
//                    return i;
//                }i
                if(detail.getSaleAmt()!=null  && detail.getIntegral()!=null && detail.getSaleAmt().compareTo(new BigDecimal("0"))==1){
                    detail.setParentId(integralRule.getId().longValue());
                    i = integralRuleDetailMapper.insertIntegralRuleDetail(detail);
                    List<IntegralTimeRuleDetail> integralTimeRuleDetails = detail.getIntegralTimeRuleDetails();
                    integralTimeRuleDetails.stream().forEach(s->s.setRuleDetailId(detail.getId()));
                    if(integralTimeRuleDetails!=null&&integralTimeRuleDetails.size()>0){
                        integralTimeRuleDetailMapper.insertBatch(integralTimeRuleDetails);
                    }
                    if(i==0){
                        return i;
                    }
                }
            }
        }

        return i;
    }

    /**
     * 修改积分规则
     *
     * @param integralRule 积分规则
     * @return 结果
     */
    @Override
    public int updateIntegralRule(IntegralRule integralRule)
    {
        //删除对应的明细表数据
        IntegralRuleDetail ruleDetail = new IntegralRuleDetail();
        ruleDetail.setParentId(integralRule.getId());
        List<IntegralRuleDetail> details=integralRuleDetailMapper.selectIntegralRuleDetailByParentId(integralRule.getId());
        for (IntegralRuleDetail detail : details) {
            integralTimeRuleDetailMapper.deleteByRuleDetailId(detail.getId());
        }
        integralRuleDetailMapper.deleteIntegralRuleDetail(ruleDetail);
        //从新保存数据
        if(integralRule.getItemsDetail()!=null &&  integralRule.getItemsDetail().size()>0){
            List<IntegralRuleDetail> list =integralRule.getItemsDetail();
            for (IntegralRuleDetail detail : list){
                /*if(detail.getOilName()!=null && detail.getOilName()!=""){
                    //查询当前油品对应的
                    String dataValue= dictDataMapper.selectDictValue("oil_name",detail.getOilName());
                    detail.setOilType(dataValue);
                }*/
                if(detail.getSaleAmt()!=null  && detail.getIntegral()!=null && detail.getSaleAmt().compareTo(new BigDecimal("0"))==1){
                    detail.setParentId(integralRule.getId().longValue());
                    integralRuleDetailMapper.insertIntegralRuleDetail(detail);
                    detail.getIntegralTimeRuleDetails().stream().forEach(s->s.setRuleDetailId(detail.getId()));
                    if(detail.getIntegralTimeRuleDetails()!=null&&detail.getIntegralTimeRuleDetails().size()>0){
                        integralTimeRuleDetailMapper.insertBatch(detail.getIntegralTimeRuleDetails());
                    }
                }
            }
        }
        integralRule.setImage1("");
        integralRule.setImage2("");
        integralRule.setImage3("");
        integralRule.setImage4("");
        integralRule.setImage5("");
        if(integralRule.getImgFileList()!=null &&  integralRule.getImgFileList().size()>0){
            for (int j=0;j<integralRule.getImgFileList().size();j++){
                if(j==0){
                    integralRule.setImage1(integralRule.getImgFileList().get(j).getUrl());
                }else if(j==1){
                    integralRule.setImage2(integralRule.getImgFileList().get(j).getUrl());
                }else if(j==2){
                    integralRule.setImage3(integralRule.getImgFileList().get(j).getUrl());
                }else if(j==3){
                    integralRule.setImage4(integralRule.getImgFileList().get(j).getUrl());
                }else if(j==4){
                    integralRule.setImage5(integralRule.getImgFileList().get(j).getUrl());
                }
            }
        }
        System.out.println("--------------------"+integralRule.getIntegralEmptyTime());
        System.out.println("--------------------"+integralRule.getEmptyDate());
        return  integralRuleMapper.updateIntegralRule(integralRule);
    }

    /**
     * 批量删除积分规则
     * insertIntegralRule
     * @param ids 需要删除的积分规则ID
     * @return 结果
     */
    @Override
    public int deleteIntegralRuleByIds(Long[] ids)
    {
        return integralRuleMapper.deleteIntegralRuleByIds(ids);
    }

    /**
     * 删除积分规则信息
     *
     * @param id 积分规则ID
     * @return 结果
     */
    @Override
    public int deleteIntegralRuleById(Long id)
    {
        return integralRuleMapper.deleteIntegralRuleById(id);
    }

    /****
     * 获取当前站的积分规则
     * @param integralRule
     * @return
     */
    @Override
    public IntegralRule selectIntegralRule(IntegralRule integralRule) {
        IntegralRule rule = integralRuleMapper.selectIntegralRule(integralRule);
        if(rule!=null){
            LoginUser currentUser = SecurityUtils.getLoginUser();
            IntegralRuleDetail  detail  = new  IntegralRuleDetail();
            detail.setParentId(rule.getId());
            List<IntegralRuleDetail> list =  integralRuleDetailMapper.selectIntegralRuleDetailList(detail);
            if(list!=null && list.size()>0){
                for(IntegralRuleDetail ruleDetail:list){
                    //查询油站的油品信息
                    List<IntegralTimeRuleDetail> timeRuleDetails=integralTimeRuleDetailMapper.queryByRuleDetailId(ruleDetail.getId());
                    ruleDetail.setIntegralTimeRuleDetails(timeRuleDetails);
                    if(ruleDetail.getOilName()!=null && !ruleDetail.getOilName().equals("")){
//                        //查询当前油品对应的所有的等级
//                        CustomerGradeSetting  customerGradeSetting =new CustomerGradeSetting();
//                        customerGradeSetting.setDelFlag("0");
//                        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
//                        customerGradeSetting.setOilName(ruleDetail.getOilName());
//                        List<CustomerGradeSetting> greadList = customerGradeSettingMapper.selectCustomerGradeSettingList(customerGradeSetting);
//                        ruleDetail.setGradeList(greadList);

                    }
                }
            }else{
                IntegralRuleDetail integralRuleDetail = new IntegralRuleDetail();
                list.add(integralRuleDetail);
            }
            rule.setItemsDetail(list);
            //查询图片信息
            List<IntegralShopPic> shopPicList =new ArrayList<>();
            if(rule.getImage1() !=null && !rule.getImage1().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("1");
                pic.setUrl(rule.getImage1());
                shopPicList.add(pic);
            }
            if( rule.getImage2() !=null && !rule.getImage2().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("2");
                pic.setUrl(rule.getImage2());
                shopPicList.add(pic);
            }
            if( rule.getImage3() !=null && !rule.getImage3().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("3");
                pic.setUrl(rule.getImage3());
                shopPicList.add(pic);
            }
            if( rule.getImage4() !=null && !rule.getImage4().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("4");
                pic.setUrl(rule.getImage4());
                shopPicList.add(pic);
            }
            if(rule.getImage5() !=null && !rule.getImage5().equals("")){
                IntegralShopPic pic =new IntegralShopPic();
                pic.setName("5");
                pic.setUrl(rule.getImage5());
                shopPicList.add(pic);
            }
            rule.setImgFileList(shopPicList);
        }else{
            rule =new IntegralRule();
            rule.setIntegralFlag("0");
            rule.setIntegralActivity("0");
            rule.setIntegralProportion("1");
            rule.setIntegralTermSetting("1");
            rule.setTermDateManage("1");
            List<IntegralRuleDetail> list = new ArrayList<IntegralRuleDetail>();
            IntegralRuleDetail integralRuleDetail = new IntegralRuleDetail();
            list.add(integralRuleDetail);
            List<IntegralShopPic> shopPicList =new ArrayList<>();
            rule.setImgFileList(shopPicList);
            rule.setItemsDetail(list);
        }
        return rule;
    }

    /**
     * 查询积分明细
     * @param integralRule
     * @return
     */
    @Override
    public List<IntegralRule> listRuleInfo(IntegralRule integralRule) {
        return integralRuleMapper.listRuleInfo(integralRule);
    }
}
